import zmq
import socket


def forward_data():
    # 创建一个 TCP socket 连接到发送数据的 socket
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect(('localhost', 12345))

    context = zmq.Context()
    socket_zmq = context.socket(zmq.PULL)
    socket_zmq.bind("tcp://*:5556")

    try:
        while True:
            data = client_socket.recv(1024)
            if not data:
                break
            print(f'接收到数据: {data.decode("utf-8")}')
    except Exception as e:
        print(f'发生错误: {e}')
    finally:
        client_socket.close()
        socket_zmq.close()
        context.term()


if __name__ == "__main__":
    forward_data()
